using System;
using System.Collections.Generic;
using System.Text;
using YB.EnterpriseLibrary.XSLTManager.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

namespace YB.EnterpriseLibrary.XSLTManager.Configuration.Design
{
    class XSLTManagerSettingsNodeBuilder : NodeBuilder
    {
        private XSLTManagerSettings xsltManagerSettings;
        private XSLTManagerNode defaultNode;

        public XSLTManagerSettingsNodeBuilder(IServiceProvider serviceProvider, XSLTManagerSettings xsltManagerSettings)
            : base(serviceProvider)
		{
            this.xsltManagerSettings = xsltManagerSettings;			
		}

		public XSLTManagerSettingsNode Build()
		{
			XSLTManagerSettingsNode rootNode = new XSLTManagerSettingsNode();
			foreach (XSLTManagerData data in xsltManagerSettings.XSLTManagers)
			{
				CreateXSLTManagerNode(rootNode, data);
			}			
			rootNode.DefaultXSLTManager = defaultNode;
			rootNode.RequirePermission = xsltManagerSettings.SectionInformation.RequirePermission;
			return rootNode;
		}

        private void CreateXSLTManagerNode(XSLTManagerSettingsNode node, XSLTManagerData data)
        {
            XSLTManagerNode xsltManagerNode = new XSLTManagerNode(data);
            node.AddNode(xsltManagerNode);
            if (xsltManagerNode.Name == xsltManagerSettings.DefaultXSLTManager) defaultNode = xsltManagerNode;
        }

    }
}
